require 'middleclass'
require 'Display'
require 'Gravity'
require 'Particle'

Asteroid = class('Asteroid', Display)

function Asteroid:initialize(imageSheet, sheet, x, y)
    local disObj = display.newImageRect(imageSheet,sheet ,20,20)
    disObj.x = x
    disObj.y = y
    Display.initialize(self, disObj) 
    physics.addBody(disObj, "dynamic", { density = 0.9, friction = 0.0, bounce = 0.6, radius = 10 })
    self.radio = 10
    --update listener
    Runtime:addEventListener("enterFrame", self)
    Runtime:addEventListener("clean", self)
    --collision listener
    self:addCollisionListener()
end

function Asteroid:enterFrame()
    Gravity.applyGravityAtmosphere(self, player.body.parent)
end

function Asteroid:collision(event)
     if (event.phase == "began") then
        if(event.other.cont.class.name == 'Planet')then
            self.body.parent.parent:insert(Particle.getParticle(self:getX(),self:getY(),0.8, 20))
            self:removeSelf()
        elseif(event.other.cont.class.name == 'Bug' )then
            event.other:removeSelf( )         
       end  
    elseif (event.phase == "ended") then
    end
    
end

function Asteroid:clean()
    Runtime:removeEventListener("enterFrame", self)
    Runtime:removeEventListener("clean", self)
end

    